package cn.edu.ctbu.sbadmin.grade.service.impl;


import cn.edu.ctbu.sbadmin.common.core.AbstractService;
import cn.edu.ctbu.sbadmin.common.utils.MD5Utils;
import cn.edu.ctbu.sbadmin.grade.dao.TeacherDao;
import cn.edu.ctbu.sbadmin.grade.domain.TeacherDO;
import cn.edu.ctbu.sbadmin.grade.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class TeacherServiceImpl extends AbstractService<TeacherDao, TeacherDO> implements TeacherService {

    @Override
    public String getPassword(Long id) {
        TeacherDO teacherDO = super.getById(id);//通过父类的getById方法获取TeacherDO
        if (teacherDO != null)
        {
            return teacherDO.getPassword();//非空则返回密码
        } else {
            return "";
        }
    }
    /**.**
    * @Description: 通过教师工号查询要导入的教师是否已经存在
    * @Param:
    * @Return:
    * @Author: HighJ
    * @Date Created in
    */

    @Override
    public Integer BatchInsertList(List<TeacherDO> list) {
        Integer success_index = 0;

        //1依次读取pageList中的数据
        for (TeacherDO item: list) {
            //2根据学号判断数据是否已经存在
            List<TeacherDO> s_nos = this.findByAttrName("t_no", item.getUsername());
            if(s_nos.size()>0){
                //已经有数据，需要写错误日志和进行进一步和处理


            }else{
                //3如果不存在，则去需要进行插入处理
                //默认的密码就是学号
                item.setPassword(MD5Utils.encrypt(item.getUsername(), item.getUsername()));   //对用户的密码在客户端进行MD5加密
                item.setGmtCreate(new Date());
                item.setIsDeleted(0);

                //返回结果
                this.save(item);
                success_index++;
            }
        }

        return success_index;
    }
    /**
     * 根据 useName取记录
     * @param userName
     * @return
     */
    @Override
    public List<TeacherDO> getByUserName(String userName){
        QueryWrapper<TeacherDO> teacherDOQueryWrapper = new QueryWrapper<TeacherDO>();
        teacherDOQueryWrapper.eq("t_no",userName);
        List<TeacherDO> teacherDOList = this.list(teacherDOQueryWrapper);
        return teacherDOList;
    }
}

